﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHMS.HelperClasses;
using WHMS.EntityClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WMS.HelperClasses;

namespace WMS
{
  
    public partial class ShelfBalanceForm : Form
    {
        

        public double AvailableBalance { get; set; }
        public double allItemsShelvesBalancesValue { get; set; }

        public List<ShelfBalance> ItemShelvesBalances { get; set; }
        
        public ShelfBalanceForm(int storeId ,int itemId,double availableBalance,int transactionId)
        {
            
            InitializeComponent();

            //this._shelves.EntityFactoryToUse = new WHMS.FactoryClasses.ShelfEntityFactory();
            this._shelfBalanceItems.EntityFactoryToUse = new WHMS.FactoryClasses.StoreShelfBalanceEntityFactory();


          

            LoadShelves(storeId);
            
            _shelfBalanceItems.RemovedEntitiesTracker = new EntityCollection<StoreShelfBalanceEntity>();
            _shelfBalanceItemsBS.DataSource = LoadItemShelves(itemId,storeId,transactionId) ;

            AvailableBalance = availableBalance;


       

            
            
        }

        public ShelfBalanceForm(List<ShelfBalance> shelvesBalances, double availableBalance, int storeId)
        {
            InitializeComponent();

            LoadShelves(storeId);
            if (shelvesBalances != null)
            {
                _shelfBalanceItemsBS.DataSource = Business.ConvertListToDataTable(shelvesBalances);
               
            }
            AvailableBalance = availableBalance;
        }

        private DataTable LoadItemShelves(int itemId, int storeId,int transactionId)
        {
            return Business.GetItemStoreShelfBalance(storeId, itemId,transactionId);
        }

        private void LoadShelves(int storeId)
        {
            
            _shelves.Clear();
            DALHelper.FetchCollection(_shelves, new RelationPredicateBucket(ShelfFields.StoreId == storeId));
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {

            if (!ValidateItemBalance())
            {
                lblError.Visible = true;

            }

            else
            {
                lblError.Visible = false;
                this.Close();
            }
        }

        private bool ValidateItemBalance()
        {
              allItemsShelvesBalancesValue=0;

              ItemShelvesBalances = new List<ShelfBalance>();
            

             foreach (DataGridViewRow row in dgvShelfBalance.Rows)
             {
                 ShelfBalance currentShelfBalance = new ShelfBalance();
                 if (row.Cells["balanceColumn"].Value != null)
                 {
                     if (!String.IsNullOrEmpty(row.Cells["balanceColumn"].Value.ToString()))
                     {
                         allItemsShelvesBalancesValue += double.Parse(row.Cells["balanceColumn"].Value.ToString());

                         currentShelfBalance.Quantity = double.Parse(row.Cells["balanceColumn"].Value.ToString());
                         currentShelfBalance.ShelfId = Int32.Parse(row.Cells["shelfCodeColumn"].Value.ToString());

                         ItemShelvesBalances.Add(currentShelfBalance);
                     }
                 }
             }
           


            if (AvailableBalance >= allItemsShelvesBalancesValue)
                return true;
            return false;
        }

       
    }

    //public class ShelfBalance
    //{
    //    public string ShelfId { get; set; }
    //    public double Balance { get; set; }
    //}

}
